package com.org.controller.page;

import com.org.entity.Poem;
import com.org.entity.Student;
import com.org.service.PoemService;
import com.org.service.UserListService;
import com.org.service.impl.GetBgIdServiceImpl;
import com.org.service.impl.JucServiceTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author Rainy-Heights
 * @version 1.0
 * @Date 2023/3/4 19:28
 */
@Controller
@RequestMapping("/page")

//主要的界面展示
public class PageController {
    @Value("${test.name}")//读取配置yaml格式或者properties的文件内容
    String data;

    //主页
    @Resource
    JucServiceTest jucServiceTest;

    @Resource
     PoemService service;

    @Resource
    UserListService userListService;

    @Resource
    GetBgIdServiceImpl bgIdService;


    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index(HttpServletRequest request,Model model){
        Poem poem=service.getPoem();


            //:测试内部异常错误
            //        if (true) throw new RuntimeException();
        jucServiceTest.yibuChart();
        System.out.println("I am the form take into the method");
        //这里主线程将会先执行，不管异步线程

        model.addAttribute("randomPoem",poem.getContent());
        model.addAttribute("authorName",poem.getAuthor());
        model.addAttribute("title",poem.getTitle());
        Logger logger= LoggerFactory.getLogger(PageController.class);
        MDC.put("reqId", request.getSession().getId());//MDC本地线程打印访问的sessionID

        logger.info(request.getSession().getId()+"get into the indexPage");
        return "/index";
    }



    //内容管理
    @RequestMapping("/contents")
    public String content(Model model){
        Poem poem=service.getPoem();

        model.addAttribute("poemList", service.getContentPoems());
        model.addAttribute("content_bg",bgIdService.getBgAddress());
        model.addAttribute("randomPoem",poem.getContent());
        model.addAttribute("authorName",poem.getAuthor());
        model.addAttribute("title",poem.getTitle());
        return "/contacts-profile";
    }

    @RequestMapping("/userList")
    public String getUser(Model model){

        Poem poem=service.getPoem();
        model.addAttribute("userList",userListService.getAllUser());
        model.addAttribute("activeNum",userListService.getActiveNum());
        model.addAttribute("randomPoem",poem.getContent());
        model.addAttribute("authorName",poem.getAuthor());
        model.addAttribute("title",poem.getTitle());

        return "/userList";
    }


    //用户管理页面
    @RequestMapping("/pages-starter.html")
    public String filter1(){
        return  "/index";
    }
    @RequestMapping("/pages-maintenance.html")
    public String filter2(){
        return  "/index";
    }


    //自动识别为json的格式
    @RequestMapping("/student")
    @ResponseBody
    public Student student(){
        Student student=new Student();
        student.setName("Merry");
        return  student;
    }
    //配置模板解析器的时候不需要配置响应体来实现
    @RequestMapping("/test")
    @ResponseBody
    public String bootTest(){
        return  "第一个SpringBoot的测试";
    }
}
